from robot.api import logger
from selenium.webdriver.common.by import By

from .antd_page_element import AntdPageElement
from ...common.element_function import ElementFunction
from ...exception.TestException import TestException
from ...exception.errorcode import ErrorCode


class CheckBox(AntdPageElement):
    def setValue(self, *values):
        """如果是单个checkbox，value不是False则是需要选中，如果是组，value是要设置的值，可以多个

        :param values:
        :return:
        """
        element = self._get_web_element()
        if 'ant-checkbox-group' in element.get_attribute('class'):
            check_box_elements = self._find_check_box_elements(element)
            for check_box_element in check_box_elements:
                name = self._get_check_box_text(check_box_element)
                if name in values:
                    if not self._is_checked(check_box_element):
                        logger.debug('to checked the {0}'.format(name))
                        ElementFunction(self._driver).click(check_box_element)
                        if not self._is_checked(check_box_element):
                            raise TestException.by_error_code(ErrorCode.ELEMENT_OPERATE_FAILED,
                                                              message='check box {0} checked failed'.format(name))
                    else:
                        logger.debug('the checkbox {0} already checked'.format(name))
                else:
                    if self._is_checked(check_box_element):
                        logger.debug('un checked the {0}'.format(name))
                        ElementFunction(self._driver).click(check_box_element)
                        if self._is_checked(check_box_element):
                            raise TestException.by_error_code(ErrorCode.ELEMENT_OPERATE_FAILED,
                                                              message='un check box {0} checked failed'.format(name))
        elif 'ant-checkbox-wrapper' in element.get_attribute('class'):
            need_checked = True if values[0] and values[0].lower() != 'false' else False
            is_checked = self._is_checked(element)
            if need_checked != is_checked:
                ElementFunction(self._driver).click(element)
                if self._is_checked(element) != need_checked:
                    raise TestException.by_error_code(ErrorCode.ELEMENT_OPERATE_FAILED,
                                                      message='change check box {0} failed'.format(element.text))
            else:
                logger.debug('the check box not need to change the status')
        else:
            raise TestException.by_error_code(ErrorCode.UN_SUPPORT_ELEMENT, selector=str(element))

    def _is_checked(self, element):
        span_element = element.find_element(By.CSS_SELECTOR, 'span.ant-checkbox')
        return 'ant-checkbox-checked' in span_element.get_attribute('class')

    def _get_check_box_text(self, element):
        # 直接取label元素的text，竟然可以！！！
        label = element.text
        return label.strip()

    def _find_check_box_elements(self, element):
        selector = 'label.ant-checkbox-wrapper'
        return element.find_elements(By.CSS_SELECTOR, selector)

    def get_identification(self):
        identification = {'class': ['ant-checkbox-group', 'ant-checkbox-wrapper']}
        return identification
